package com.cjb.cloudfabric.controller;

import cn.hutool.json.JSONObject;
import com.cjb.cloudfabric.entity.BasicAsset;
import com.cjb.cloudfabric.entity.FabricUser;
import com.cjb.cloudfabric.service.OperateFabricService;
import com.cjb.cloudfabric.util.PDFUtil;
import com.cjb.common.util.JsonUtils;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class AssetController
 * @Date 2023-10-17 下午9:35
 */
@RestController
@RequestMapping(value = "asset")
@Tag(name = "资产交易", description = "资产交易 API")
@AllArgsConstructor
public class AssetController {
    /**
     * 日志
     */
    private final static Logger log = LoggerFactory.getLogger(AssetController.class);
    /**
     * 注入资产API
     */
    private final OperateFabricService operateFabricService;

    /**
     *
     * 查看全部资产列表
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("/queryAllAsset")
    @Operation(summary = "查询全部资产")
    public Result<String> queryAllAsset() throws Exception {
        log.info("获得请求");
        return Result.success(operateFabricService.QueryAllAsset());
    }

    /**
     *
     * 查看全部资产列表
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PutMapping("/addAsset")
    @Operation(summary = "新增资产")
    public Result<String> addAsset(@RequestBody BasicAsset basicAsset) throws Exception {
        log.info("新增资产");
        return Result.success(operateFabricService.SubmitAllAsset(basicAsset));
    }

    /**
     * 通过assetid获取资产
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("queryById")
    @Operation(summary = "通过ID查询资产")
    public Result<String> relQueryById(@RequestBody BasicAsset basicAsset) throws Exception {
        log.info("id查询资产");
        return Result.success(basicAsset.getId() == null ? "" : operateFabricService.QueryAssetById(basicAsset.getId()));
    }

    /**
     * 通过assetid获取资产 -- 真刪除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("relDeleteById")
    @Operation(summary = "删除资产")
    public Result<String> relDeleteById(@RequestBody BasicAsset basicAsset) throws Exception {
        log.info("删除资产");
        return Result.success(basicAsset.getId() == null ? "fail" : operateFabricService.DeleteAsset(basicAsset.getId()));
    }

   /**
    *  剩下的都是修改合约 就是资产的转移
    */

    /**
     * 服务证书生成
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("becomeCertificate")
    @Operation(summary = "颁发生成证书")
    public Result<String> becomeCertificate(@RequestBody FabricUser fabricUser){
        try {
            operateFabricService.becomeCertificate(fabricUser);
        } catch (Exception e) {
            log.info("出现异常...");
            throw new RuntimeException(e);
        }
        return Result.success();
    }

}
